package md.lanjing.com.module_wallet.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_wallet.R;
import md.lanjing.com.module_wallet.R2;
import md.lanjing.com.module_wallet.bean.TransferRecordResponce;
import md.lanjing.com.module_wallet.contact.TransferRecordContact;
import md.lanjing.com.module_wallet.ui.adapter.TranferRecordAdapter;
import retrofit2.Response;

public class TransferRecordFragment extends MvpFragment<TransferRecordContact.TransferRecordPresent> implements TransferRecordContact.ITransferRecordView {
    @BindView(R2.id.rv)
    RecyclerView rv;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    private int pageNo=1;

    List<TransferRecordResponce.DataBean> mList;
    TranferRecordAdapter mAdapter;
    @Override
    public void initData(Bundle savedInstanceState) {
        mList=new ArrayList<>();
        mAdapter=new TranferRecordAdapter(R.layout.recy_item_transfer_record_list,mList);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);

        mPresent.queryTransferRecordList(context,pageNo);
        refresh.setOnRefreshListener(refreshLayout -> {
            pageNo=1;
            mPresent.queryTransferRecordList(context,pageNo);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNo++;
            mPresent.queryTransferRecordList(context,pageNo);
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_transfer_record;
    }


    @Override
    protected TransferRecordContact.TransferRecordPresent createPresent() {
        return new TransferRecordContact.TransferRecordPresent();
    }

    @Override
    public void getRecordListData(Response<TransferRecordResponce> response) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            List<TransferRecordResponce.DataBean> data = response.body().getData();
            if (pageNo==1){
                mList.clear();
            }
            mList.addAll(data);
            mAdapter.notifyDataSetChanged();
            if (data!=null&&data.size()<10){
                refresh.setEnableLoadMore(false);
            }else {
                refresh.setEnableLoadMore(true);
            }
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }
}
